<template>
    <el-form :model="user" label-width="100px" class="form">
        <el-form-item label="用户名：">
            <el-input style="width: 220px;" v-model="user.userName" placeholder="用户名"  @blur="qc()" ></el-input>
        </el-form-item>
        <el-form-item label="密码：">
            <el-input style="width: 220px;" type="password" v-model="user.userPwd" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item label="员工：">
            <el-select v-model="user.hrEmpId" placeholder="请选择员工" style="width: 220px;">
                <el-option v-for="emp in emps" :label="emp.empName" :key="emp.id" :value="emp.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="角色：">
            <el-select v-model="user.roleId" placeholder="请选择角色" style="width: 220px;">
                <el-option v-for="role in roles" :label="role.roleName" :key="role.id" :value="role.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button @click="add">添加</el-button>
        </el-form-item>
    </el-form>
</template>

<script>

    import Hr_empService from '../../../model/hyc/HrEmpService.js'
    const  hr_empService =Hr_empService.getInstance();

    import  SystemUserService from '../../../model/mfy/system_user/SystemUserService.js'
    const  systemUserService =SystemUserService.getInstance();

    import  SystemRoleService from '../../../model/mfy/system_user/SystemRoleService.js'
    const  systemRoleService =SystemRoleService.getInstance();

    export default {
        name: "add_user",
        data(){
            return{
                user:{},
                emps:[],
                roles:[],
            }
        },
        methods:{
            add(){

                if(this.user.userName==null || this.user.userName == "" ){
                    this.$message.error("用户名不能为空!")
                    return;
                }
                if(this.user.userPwd==null || this.user.userPwd == "" ){
                    this.$message.error("用户密码不能为空!")
                    return;
                }
                if(this.user.hrEmpId==null || this.user.hrEmpId == "" ){
                    this.$message.error("员工不能为空!")
                    return;
                }
                if(this.user.roleId==null || this.user.roleId == "" ){
                    this.$message.error("员工角色不能为空!")
                    return;
                }

                systemUserService.save(this.user).then(()=>{
                    this.$emit("refresh")
                })
            },
            qc(){
                systemUserService.qc(this.user.userName).then((response)=>{
                    if(response.data.data!=null){
                        this.$message.error("用户名已经重复了!")
                        this.user.userName=""
                        return;
                    }
                })
            },
            getEmps(){
                hr_empService.listByUserIDisNull().then((response)=>{
                    this.emps=response.data.data
                })

            },
            getRoles(){
                systemRoleService.list2().then((response)=>{
                    var roles2=[]
                    response.data.data.forEach(i=>{
                        if(i.roleStatus=='N'){
                            return
                        }else{
                            roles2.push(i)
                        }
                    })
                    this.roles=roles2;
                })
            },
        },
        created(){
            this.getEmps()
            this.getRoles()
        }
    }
</script>

<style scoped>
</style>
